package com.cw.controller;

import com.cw.annotation.AutoYard;
import com.cw.annotation.OperationLogAnno;
import com.cw.common.api.ResultObject;
import com.cw.common.method.QueueNumberMsg;
import com.cw.dto.InpatientRegistrationCondition;
import com.cw.dto.LeaveAdviceDTO;
import com.cw.dto.PreoperationDTO;
import com.cw.dto.PrepayinDTO;
import com.cw.dto.ReleaseJGBedHistoryDTO;
import com.cw.entity.CwglBedtypechange;
import com.cw.entity.ThisIiPrepayin;
import com.cw.request.BedChangeTypeRequest;
import com.cw.request.EemptyPatientRequest;
import com.cw.service.IThisIiPrepayinService;
import com.cw.util.PageHelperTool;
import com.cw.vo.LeaveAdviceVO;
import com.cw.vo.QueueNumberVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author smq
 * @since 2021-05-06
 */
@Api(tags = {"住院预约表"})
@RestController
@RequestMapping("/this-ii-prepayin")

public class ThisIiPrepayinController {

    @Autowired
    private IThisIiPrepayinService targetService;


    @AutoYard
    @ApiOperation("住院列表")
    @PostMapping("/getThisIiPrepayinPagination")
    public ResultObject getThisIiPrepayinPagination(@RequestBody InpatientRegistrationCondition condition) {
        return ResultObject.successReturn(targetService.getThisIiPrepayinPagination(condition));
    }


    @AutoYard
    @ApiOperation("住院列表-住院预约记录列表")
    @PostMapping("/getTheInpatientOfficeHasBeenProcessed")
    public ResultObject getTheInpatientOfficeHasBeenProcessed(@RequestBody InpatientRegistrationCondition condition) {
        return ResultObject.successReturn(targetService.getTheInpatientOfficeHasBeenProcessed(condition));
    }

    @GetMapping("/getThisIiPrepayin")
    @ApiOperation("详情")
    public ResultObject getThisIiPrepayin(@RequestParam String id) {
        //11-28
        ThisIiPrepayin iiPrepayin = targetService.getById(id);
        if (iiPrepayin == null) {
            return ResultObject.errorReturn("未查询到该患者电子住院证信息，请联系信息处重推一下！");
        }
        return ResultObject.successReturn(iiPrepayin);
    }


    @ApiOperation(value = "获取没有床位的患者信息")
    @PostMapping("/getEemptyPatientListNew")
    public ResultObject getEemptyPatientListNew(@RequestBody EemptyPatientRequest emptyPatientRequest) {
        return ResultObject.successReturn(targetService.getEemptyPatientListNew(emptyPatientRequest));
    }

    @ApiOperation(value = "获取所有没有床位的患者信息")
    @PostMapping("/getEemptyPatientListAll")
    public ResultObject getEemptyPatientListAll(@RequestBody EemptyPatientRequest emptyPatientRequest) {
        return ResultObject.successReturn(targetService.getEemptyPatientListAll(emptyPatientRequest));
    }

    /**
     * /ManualElecHosptalCertificate/index开立电子住院证
     * @param prepayinDTO
     * @return
     * @throws Exception
     */
    @ApiOperation("手工录入")
    @PostMapping("/insertIiPrepayin")
    public ResultObject insertIiPrepayin(@RequestBody PrepayinDTO prepayinDTO) throws Exception {
        return targetService.insertIiPrepayin(prepayinDTO);
    }

    @ApiOperation("预手术查询")
    @PostMapping("/preoperation")
    public ResultObject preoperation(@RequestBody PreoperationDTO dto) {
     return ResultObject.successReturn(targetService.preoperation(dto));
    }

    @ApiOperation("编辑手工录入")
    @PostMapping("/editIiPrepayin")
    public ResultObject editIiPrepayin(@RequestBody PrepayinDTO prepayinDTO) {
        return targetService.editIiPrepayin(prepayinDTO);
    }

    @ApiOperation("通过clinicCode获取患者的基本信息")
    @GetMapping("/getInsertIiPrepayinInsertSource")
    public ResultObject getInsertIiPrepayinInsertSource(@RequestParam String clinicCode) {
        return targetService.getInsertIiPrepayinInsertSource(clinicCode);
    }

    @ApiOperation("将未预约的住院预约记录作废")
    @GetMapping("/updateStatus")
    @OperationLogAnno(site = "住院预约记录", operation = "未预约->作废", bz = "门诊办人员操作",
            method = "/this-ii-prepayin/updateStatus", type = "UPDATE")
    public ResultObject updateStatus(@RequestParam Integer preId) {
        return ResultObject.successReturn(targetService.updateState(preId));
    }

    @AutoYard
    @ApiOperation("历史记录列表")
    @PostMapping("/historyRecode")
    public ResultObject historyRecode(@RequestBody InpatientRegistrationCondition condition) {
        return ResultObject.successReturn(targetService.historyRecord(condition));
    }

    @ApiOperation("修改床位属性")
    @PostMapping("/changeBedType")
    @OperationLogAnno(site = "修改床位集管非集管属性", operation = "集管非集管属性变更", bz = "门诊办人员操作",
            method = "/this-ii-prepayin/changeBedType", type = "UPDATE")
    public ResultObject changeBedType(@RequestBody List<CwglBedtypechange> listInfo) {
        boolean retBoolean = true;
        try {
            retBoolean = targetService.changeBedType(listInfo);
        } catch (Exception ex) {
            retBoolean = false;
        }
        return ResultObject.successReturn(retBoolean);
    }

    @ApiOperation("修改床位属性(病区)")
    @GetMapping("/changeBedTypeByNurseCode")
    public ResultObject changeBedTypeByNurseCode(String nurseCode) {
        boolean retBoolean = true;
        try {
            retBoolean = targetService.changeBedTypeByNurseCode(nurseCode);
        } catch (Exception ex) {
            retBoolean = false;
        }
        return ResultObject.successReturn(retBoolean);
    }

    /**
     * 床位类型转换查询
     * xnd 2021010-13
     *
     * @return
     */
    @ApiOperation("一键还原页面的查询")
    @PostMapping("/selectCwInformation")
    public ResultObject selectCwInformation(@RequestBody BedChangeTypeRequest request) {
        return ResultObject.successReturn(targetService.selectCwInformation(request));
    }


    /**
     * 释放历史床位
     * cww 20220525
     *
     * @return
     */
    @ApiOperation("释放历史床位")
    @PostMapping("/releaseJGBedHistory")
    @OperationLogAnno(site = "住院预约记录", operation = "释放历史床位", bz = "门诊办人员操作",
            method = "/this-ii-prepayin/releaseJGBedHistory", type = "UPDATE")
    public ResultObject releaseJGBedHistory(@RequestBody ReleaseJGBedHistoryDTO request) {
        return targetService.releaseJGBedHistory(request);
    }

    /**
     * 根据preid获取电子住院证
     * cww 20221117
     *
     * @return
     */
    @ApiOperation("根据preid获取电子住院证")
    @GetMapping("/getThisIiPrepayinByPreid")
    public ResultObject getThisIiPrepayinByPreid(BigDecimal preid) {
        return targetService.getThisIiPrepayinByPreid(preid);
    }


    @ApiOperation("出院医嘱统计分页查询")
    @PostMapping("/leaveAdvicePage")
    public ResultObject leaveAdvicePage(@RequestBody LeaveAdviceDTO dto) {
        List<LeaveAdviceVO> leaveAdviceList = targetService.leaveAdvicePage(dto);
        PageInfo<LeaveAdviceVO> leaveAdvicePageList = PageHelperTool.excutePageInfo(leaveAdviceList,
                dto.getPageNum(), dto.getPageSize());
        return ResultObject.successReturn(leaveAdvicePageList);
    }

    @ApiOperation("获取专业组下拉框")
    @GetMapping("/getmedicalGroup")
    public ResultObject getmedicalGroup(String deptCode) {
        return ResultObject.successReturn(targetService.getmedicalGroup(deptCode));
    }

    @ApiOperation("获取医疗组组长下拉框")
    @GetMapping("/getmedicalGroupLeader")
    public ResultObject getmedicalGroupLeader(String medicalGroupCode,String deptCode ) {
        return ResultObject.successReturn(targetService.getmedicalGroupLeader(medicalGroupCode,deptCode));
    }


    @ApiOperation("排队取号")
    @GetMapping("/getQueueNum")
    public ResultObject getQueueNum(String areaId) throws IOException {
        return ResultObject.successReturn(QueueNumberMsg.getQueueNum(areaId));
    }

    /**
     * 获取上一次出院时间与这次开立住院证的时间差
     * 如果小于等于 15 提醒
     * 提示内容“患者XX年XX月XX日，在我院XX科医保(自费)办理出院，出院诊断XX，是否继续开住院证
     *
     * @param idenNo
     * @return
     */
    @ApiOperation("获取上一次出院时间与这次开立住院证的时间差")
    @GetMapping("/getReadmissionDays")
    public ResultObject getReadmissionDays(String idenNo, String predateString) throws IOException {
        DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // String转Date
        try {
            Date predate = format2.parse(predateString);
            return targetService.getReadmissionDays(idenNo,predate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}

